var codeId = getQueryString("ids");
var id = getQueryString("id");
var url = '../sendrecording/list?codeId=' + codeId;
if (id) {
    url = '../sendrecording/list?id=' + id
}
$(function () {
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '派工人员', name: 'sendPersonName', index: 'SEND_PERSON', width: 80},
            {label: '报修编号/巡检编号Id', name: 'codeId', index: 'CODE_ID', width: 80},
            {
                label: '是否接单', name: 'whetherOrders', index: 'WHETHER_ORDERS', width: 80, formatter: function (value) {
                    return sysEnums.WHETHERORDERS[value]
                }
            },
            {
                label: '派工时间', name: 'sendDate', index: 'SEND_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '工单接受时间',
                name: 'workAcceptDate',
                index: 'WORK_ACCEPT_DATE',
                width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '工单接收人', name: 'workAcceptPersonName', index: 'WORK_ACCEPT_PERSON', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            /* {label: '${column.comments}', name: 'planId', index: 'PLAN_ID', width: 80},*/
            /*{label: '是否删除(1:未删除，0：删除)', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sendRecording: {
            sendPerson: '',
            codeId: '',
            id: '',
            urgeFlag: '',
            planId: '',
            delFlag: '',
            remark: '',
            whetherOrders: '',
            workAcceptDate: '',
            sendDate: '',
            workAcceptPerson: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.sendRecording = {
                sendPerson: '',
                codeId: '',
                id: '',
                urgeFlag: '',
                planId: '',
                delFlag: '',
                remark: '',
                whetherOrders: '',
                workAcceptDate: '',
                sendDate: '',
                workAcceptPerson: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.sendRecording.id == (null || '') ? "../sendrecording/save" : "../sendrecording/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.sendRecording),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sendrecording/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sendrecording/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.sendRecording = r.sendRecording;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        /**
         * 工单接收
         * @param params
         */
        sendClick: function (params) {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            var whetherOrders = rows.whetherOrders;
            if (ids == null) {
                return;
            }
            if (whetherOrders == "是") {
                alert('工单已接收,请勿重复操作!', function (index) {
                    vm.reload();
                });
                return;
            }
            confirm('确定要接收这条工单？', function () {
                Ajax.request({
                    url: '../inspectionplan/upReceiveSend',
                    type: 'POST',
                    data: {'id': ids},
                    successCallback: function (val) {
                        alert(val.msg, "function");
                        vm.data2 = [];
                        Ajax.request({
                            url: '../inspectionplan/queryListSend',
                            type: 'GET',
                            successCallback: function (value) {
                                for (var dataList of value.allList) {
                                    dataList.sendDate = transDate(dataList.sendDate, 'yyyy-MM-dd hh:mm');
                                    dataList.workAcceptDate = transDate(dataList.workAcceptDate, 'yyyy-MM-dd hh:mm');
                                    dataList.whetherOrders = sysEnums.IS_LEADER[dataList.whetherOrders];
                                    dataList.urgeFlag = sysEnums.IS_LEADER[dataList.urgeFlag];
                                    vm.data2.push(dataList);
                                }
                            }
                        })
                    }
                });
            });
        },
        /**
         * 催办
         */
        urgeClick: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            var urgeFlag = rows.urgeFlag;
            if (ids == null) {
                return;
            }
            if (urgeFlag == "已催办") {
                alert('工单已催办,请勿重复操作!', function (index) {
                    vm.reload();
                });
                return;
            }
            $.ajax({
                url: '../inspectionplan/upUrge',
                type: 'POST',
                data: {'id': ids},
                success: function (val) {
                    alert(val.msg, "function");
                    vm.reload();
                }
            })
        },
        addError: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 异常记录',
                content: '../sys/exceperror.html'
            });
        }
    }
});
